package com.atxiaodei.minio.controller;

import com.atxiaodei.minio.entity.FileResults;
import com.atxiaodei.minio.entity.MinIoUploadVo;
import com.atxiaodei.minio.service.MinIoService;
import io.minio.messages.DeleteObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName UploadController
 * @Deacription 基于文件对象存储方案
 * @Author 王盟
 * @Date 2021/1/14 13:54
 * @Version 1.0
 **/
@CrossOrigin
@RestController
@Slf4j
@RequestMapping("/storage")
@Api("基于文件对象存储方案")
public class ObjectStorageController {


    @Autowired
    private MinIoService minIoService;

    @Value("${minio.bucketName}")
    private String bucketName;


    @ApiOperation(value = "文件上传公共组件", notes = "基于所有类型文件上传公共组件")
    @RequestMapping(value = "/upload" , method = RequestMethod.POST)
    @ResponseBody
    public FileResults upload(MultipartFile file, HttpServletRequest request) throws IOException {
        String objectName=file.getOriginalFilename();
        InputStream inputStream = file.getInputStream();
        String strDir= request.getParameter("bucketName")==null?"test":request.getParameter("bucketName");

        try {
            MinIoUploadVo uploadVo = minIoService.upload(strDir,file);
            return  new FileResults(true,200,"文件上传成功",uploadVo);
        } catch (Exception e) {
            log.error("上传文件失败，msg={}",e.getMessage());
            e.printStackTrace();
            return  new FileResults(false,500,"网络异常",null);
        }
    }

    @ApiOperation(value = "文件下载公共组件", notes = "基于所有类型文件下载公共组件")
    @RequestMapping(value = "/download", method = RequestMethod.GET)
    public void download(MinIoUploadVo minIoUploadVo, HttpServletResponse response)  {

        try {
            minIoService.download(response, minIoUploadVo.getBucketName(), minIoUploadVo.getMinFileName());
        } catch (Exception e) {
            log.error("下载文件失败，msg={}",e.getMessage());
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "文件服务器文件删除（支持多个）", notes = "文件服务器删除公共组件")
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public FileResults delete(MinIoUploadVo minIoUploadVo)  {

        try {
            List<DeleteObject> objectList= new ArrayList<>();

            String fileDir = minIoUploadVo.getBucketName().concat("/");
            Arrays.stream(minIoUploadVo.getMinFileName().split(",")).forEach(name -> objectList.add(new DeleteObject(fileDir.concat(name))));

            minIoService.removeObject(bucketName, objectList);

            return  new FileResults(true,200,"删除文件成功",null);
        } catch (Exception e) {
            log.error("删除文件失败，msg={}",e.getMessage());
            e.printStackTrace();
            return  new FileResults(true,500,"网络异常",null);
        }
    }




}
